<template>
  <base-breadcrumb>
    <searchData :search-data="searchArr" @search="search" />
    <a-card :bordered="false">
      <!-- <div class="table-operator">
        <div class="pub-table-action-btn">
          <a-button
            v-btn-permission="'mm_034_add_01'"
            type="primary"
            icon="plus"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>

          <excel
            ref="exportSelect"
            file-name="采购效益率"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportSelect"
            >
              导出选中
            </a-button>
          </excel>
          <excel
            ref="exportAll"
            file-name="采购效益率"
            is-click-generate="false"
          >
            <a-button
              slot="excelBtn"
              @click="exportAll"
            >
              导出全部
            </a-button>
          </excel>
        </div>
        <div class="pub-table-select-count">
          <a-icon
            type="info-circle"
            theme="twoTone"
          />
          已选择{{ selectedRows.length }}项
          <span style="padding-left: 10px;">
            总计：{{ totalSize }}，
            待提交审核：{{ statistics.draft }}，
            审核中：{{ statistics.submitted }}，
            审核通过：{{ statistics.past }}，
            审核不通过：{{ statistics.rejected }}，
            已作废：{{ statistics.delete }}
          </span>
        </div>
      </div> -->
      <list-table 
        ref="table"
        :columns="columns"
        :request="loadData"
        :start-index="0"
        :out-selected-row-keys="selectedRowKeys"
        :statistics-config="{ use: true, statistics }"
        :exportConfig="{ use: true,select: '导出选中',all: '导出全部'}"
      >
       <!-- 新增 -->
       <a-button
            slot="prefix"
            v-btnPermission="'mm_034_add_01'"
            class="mb12"
            icon="plus"
            type="primary"
            @click="handleEdit(null,1)"
          >
            新增
          </a-button>
        <template slot="index" slot-scope="text, record, index">
          {{ index + 1 }}
        </template>
        <template slot="auditStatusStr" slot-scope="text, record">
          <list-status :status="record.auditStatus" :text="text" />
        </template>
        <div slot="operation" slot-scope="text, record">
          <MoreButton>
          <a @click="handleEdit(record, 3)">查看</a>
          <template v-if="(record.auditStatus === 'DRAFT' || record.auditStatus === 'REJECTED')&&hideBuquBtn()">
            <a @click="() => handleEdit(record, 2)">编辑</a>
            <a @click="() => handleDel(record.id)">删除</a>
          </template>
          <approval-button :id="record.id" :status="record.auditStatus"
                           v-if="record.auditStatus === 'PAST' || record.auditStatus === 'SUBMITTED' || record.auditStatus === 'REJECTED' || record.auditStatus === 'REVOKEIN' || record.auditStatus === 'REVOKE' || record.auditStatus === 'ABOLISH' || record.auditStatus === '1' || record.auditStatus === '3' || record.auditStatus === 'COMMIT'|| record.auditStatus === '2' " />
          </MoreButton>
        </div>
      </list-table>
      <!-- <pagination
        v-model="pageNo"
        :total="totalSize"
        :page-size.sync="pageSize"
        @change="handlePageChange"
        @showSizeChange="showSizeChange"
      /> -->
    </a-card>
  </base-breadcrumb>
</template>
<script>
import benefitAnalysis from '@/api/material/benefitAnalysis/defalut'
import SelectSubCompany from '@/components/Select/SelectSubCompany'
import SelectProject from '@/components/Select/SelectProject'

export default {
  name: 'a' + Date.now(),
  data: function () {
    return {
      loading: false,
      queryParam: {},
      pageNo: 1,
      pageSize: 10,
      totalSize: 0,
      searchArr: [
        {
          name: '采购效益分析编码',
          value: 'code',
          type: 'input'
        },
        {
          name: '起始时间',
          value: 'startDate',
          type: 'timeRangeFormat'
        },
        {
          name: '截止时间',
          value: 'endDate',
          type: 'timeRangeFormat'
        },
        {
          name: '创建人',
          value: 'creatorName',
          type: 'input'
        },
        {
          name: '创建时间',
          value: 'createTime',
          type: 'timeRangeFormat'
        },
        {
          name: '状态',
          value: 'auditStatus',
          type: 'select',
          data: this.STATUS
        }
      ],
      columns: [
        {
          title: '序号',
          dataIndex: 'index',
          scopedSlots: { customRender: 'index' },
          width: 70
        },
        {
          title: '采购效益分析编码',
          dataIndex: 'code',
          width: 150
        },
        {
          title: '项目名称',
          dataIndex: 'project',
          width: 150
        },

        {
          title: '起始时间',
          dataIndex: 'startDate',
          width: 120
        },
        {
          title: '截止时间',
          dataIndex: 'endDate',
          width: 120
        },
        {
          title: '创建人',
          dataIndex: 'creatorName',
          width: 160
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          width: 160
        },
        {
          title: '状态',
          width: this.STATUS_WIDTH,
          dataIndex: 'auditStatusStr',
          scopedSlots: { customRender: 'auditStatusStr' }
        },
        {
          title: '操作',
          dataIndex: 'operation',
          width: 200,
          fixed: 'right',
          scopedSlots: { customRender: 'operation' }
        }
      ],
      selectedRowKeys: [],
      selectedRows: [],
      statistics: {},
      tableData: [],
      loadData: (params) => {
        let searchData = Object.assign(params, this.queryParam)
        benefitAnalysis.findListCount(searchData).then((res) => {
          if (res.code === this.SUCCESS_CODE) this.statistics = res.data
        })
        return benefitAnalysis.findListByMain(searchData).then((res) => {
          return res
        })
      },
      pathName: this.$route.path
    }
  },
  mounted:async function () {
    // await this.list()
    // this.$refs.exportSelect.tableToExcelFile(this.$refs.table)
    // this.$refs.exportAll.tableToExcelFile(this.$refs.table)
    // console.log('mounted-----------table的Path', this.pathName)
    this.$refs.table.refresh()
    // 监听详情页返回刷新列表
    //this.pathName取的值是this.$route.path
    this.$bus.$on('handleRefresh' + this.pathName, () => {
      console.log('--------------------收到了，刷新刷新刷新----------------', this.pathName)
      // this.list()
      this.$refs.table.refresh()
    })

    let res = await benefitAnalysis.labelTreeList()
    console.log('rrr', res)
  },
  beforeDestroy() {
    console.log('beforeDestroy-------table的Path', this.pathName)
    this.$bus.$off('handleRefresh' + this.pathName)
  },
  methods: {
    exportSelect() {
      if (this.selectedRows.length > 0) {
        this.$refs.exportSelect.setDataAndGenerate(this.selectedRows)
      } else {
        this.$message.warning('请选择需要导出的数据')
      }
    },
    exportAll() {
      // 参数：查询条件、导出的回调
      if (this.totalSize) {
        this.pageNo = 1
        this.list(false, this.$refs.exportAll.setDataAndGenerate)
      } else {
        this.$message.warning('没有可导出的数据')
      }
    },
    invalidResult(row) {
      this.invalidList(invalid, this.search, { id: row.id })
    },
    async list(params, callback) {
      this.loading = true
      let searchData = Object.assign(
        {
          pageSize: callback ? this.totalSize : this.pageSize,
          pageNo: this.pageNo
        },
        params,
        this.queryParam
      )
      benefitAnalysis.findListCount(searchData).then((res) => {
        if (res.code === this.SUCCESS_CODE) this.statistics = res.data
      })
      let res = await benefitAnalysis.findListByMain(searchData)
      // this.$console(res)
      if (res.code === 200) {
        res.data.map((v) => (v.id += ''))
        if (callback) {
          if (res.data.length) {
            callback(res.data)
          } else {
            this.$message.warning('没有可导出的数据')
          }
          return
        }
        this.tableData = res.data
        this.totalSize = res.totalSize
      }
      this.loading = false
    },
    search(val) {
      this.pageNo = 1
      this.queryParam = val
      this.$refs.table.refresh()
      // this.list(val)
      // this.selectedRowKeys = []
    },
    // handlePageChange(pageNo, pageSize) {
    //   this.pageNo = pageNo
    //   this.list()
    //   this.selectedRowKeys = []
    // },
    // showSizeChange(pageNo, pageSize) {
    //   this.pageNo = 1
    //   this.pageSize = pageSize
    //   this.list()
    //   this.selectedRowKeys = []
    // },
    // onSelectChange(selectedRowKeys, selectedRows) {
    //   this.selectedRows = selectedRows
    //   this.selectedRowKeys = selectedRowKeys
    // },
    handleDel(id) {
      let params = id ? [id] : this.selectedRowKeys
      this.delList2(
        benefitAnalysis.deleteByBenefitAnalysis,
        () => {
          this.search()
          this.selectedId = []
          this.selectedRowKeys = []
          this.selectedRows = []
          this.editingKey = ''
        },
        { ids: params }
      )
    },
    handleEdit(record, state) {
      this.$router.push({
        path: '/material/benefitAnalysis/add',
        query: {
          id: record && record.id,
          state: state
        }
      })
    }
  }
}
</script>
